from django.contrib import admin
from django import forms
from coatl.blog.models import Post, Theme
admin.autodiscover()

class PostAdmin(admin.ModelAdmin):
  search_fields = ('title', 'post')
  list_filter   = ('title', 'date')
  list_display  = ('theme', 'date', 'title')  
  ordering      = ('-date', 'title')

  def was_published_today(self):
      return self.pub_date.date() == datetime.date.today()
      #was_published_today.short_description = 'Published today?'

  def save_model(self, request, obj, form, change):
      obj.author_id = request.user.id
      obj.save()

  def queryset(self, request):
     # if request.user.is_superuser:
     #    qs = self.model._default_manager.get_query_set()
     # else:
     qs = self.model._default_manager.get_query_set().filter(author = request.user)

     ordering = self.ordering 
     """ or ()
                                   if ordering:
                                       qs = qs.order_by(*ordering) """
     return qs

#Validate
class MyPostAdminForm(forms.ModelForm):
    class Meta:
        model = Post

    def clean_title(self):
        # do something that validates your data
        return self.cleaned_data["title"]

admin.site.register(Post, PostAdmin)
admin.site.register(Theme)
